package com.mzj.saas.mservice.auth.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dypnsapi.model.v20170525.DescribeVerifySchemeRequest;
import com.aliyuncs.dypnsapi.model.v20170525.DescribeVerifySchemeResponse;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileRequest;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.auth.vo.InitNumberVerifyVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class InitNumberVerifyService {
    @Value("${ali.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${ali.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${app.scheme.code}")
    private String SchemeCode;

    /**
     * 开始认证前通过本接口获取AppEncryptInfo
     * @return
     * @author xin
     * @date 2022年6月27日
     */
    public ResultBean<Object> getAppEncryptInfo(String schemeCode) throws Exception {
        log.info("getAppEncryptInfo schemeCode={}",schemeCode);
        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        DescribeVerifySchemeRequest request = new DescribeVerifySchemeRequest();
        if (StringUtils.isNotEmpty(schemeCode)){
            request.setSchemeCode(schemeCode);
        }else {
            request.setSchemeCode(SchemeCode);
        }

        try {
            DescribeVerifySchemeResponse response = client.getAcsResponse(request);
            log.info(new Gson().toJson(response));
            return ResultBean.successfulResult(response.getSchemeQueryResultDTO().getAppEncryptInfo());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            log.error("ErrCode:" + e.getErrCode());
            log.error("ErrMsg:" + e.getErrMsg());
            log.error("RequestId:" + e.getRequestId());
        }



        return ResultBean.failedResultOfException();
    }


    /**
     * 获得手机号
     * @return
     * @author xin
     * @date 2022年6月27日
     */
    public ResultBean<Object> getMobile(InitNumberVerifyVo vo) {
        log.info("getMobile vo={}", JSONObject.toJSON(vo));
        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", accessKeyId, accessKeySecret);

        IAcsClient client = new DefaultAcsClient(profile);
        GetMobileRequest request = new GetMobileRequest();
        request.setAccessToken(vo.getAccessToken());
        try {
            GetMobileResponse response = client.getAcsResponse(request);
            log.info(new Gson().toJson(response));
            return ResultBean.successfulResult(response);
        } catch (ServerException e) {
            log.error(String.valueOf(e));
            e.printStackTrace();
        } catch (ClientException e) {
            log.error("ErrCode:" + e.getErrCode());
            log.error("ErrMsg:" + e.getErrMsg());
            log.error("RequestId:" + e.getRequestId());
        }

        return ResultBean.failedResultOfException();
    }

}
